package com.maixy.studyonlineapi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.entity.Activity;
import com.maixy.studyonlineapi.entity.ActivityContent;
import com.maixy.studyonlineapi.entity.ActivityContentBrowseRecord;
import com.maixy.studyonlineapi.entity.Auth;
import com.maixy.studyonlineapi.mapper.ActivityContentBrowseRecordMapper;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.utils.LocalDateTimeUtils;
import com.maixy.studyonlineapi.view.ActivityContentBrowseRecordView;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName ActivityContentBrowseRecordService
 * @Description TODO
 * @Date 2021/2/7 23:19
 * @Author maixy
 */
@Service
public class ActivityContentBrowseRecordService extends BaseMPlusService<ActivityContentBrowseRecord, Long>{

    private final ActivityContentBrowseRecordMapper activityContentBrowseRecordMapper;

    @Resource
    private ActivityContentService activityContentService;
    @Resource
    private ActivityService activityService;
    @Resource
    private AuthService authService;
    @Resource
    private TeamService teamService;


    protected ActivityContentBrowseRecordService(ActivityContentBrowseRecordMapper activityContentBrowseRecordMapper) {
        super(ActivityContentBrowseRecord.class, activityContentBrowseRecordMapper);
        this.activityContentBrowseRecordMapper = activityContentBrowseRecordMapper;
    }

    /**
     * 添加浏览课件记录
     *
     * @param userId 用户id
     * @param activityId 活动id
     * @param activityContentId 课件id
     */
    public void addBrowseRecord(Long userId, Long activityId, Long activityContentId) {
        Activity activity = activityService.findById(activityId);
        if (null == activity || activity.getHasDelete()) {
            return;
        }
        ActivityContent activityContent = activityContentService.findContentByIdAndActivityId(activityContentId, activityId);
        if (null == activityContent || activityContent.getHasDelete()) {
            return;
        }
        Auth auth = authService.findAuthByUserIdAndActivityId(userId, activityId);
        if (null == auth || auth.getHasDelete()) {
            return;
        }
        ActivityContentBrowseRecord activityContentBrowseRecord = findBrowseRecord(userId, activityContentId);
        if (null != activityContentBrowseRecord && !activityContentBrowseRecord.getHasDelete()) {
            return;
        }
        ActivityContentBrowseRecord record = new ActivityContentBrowseRecord();
        record.setCreateBy(userId);
        record.setCreateTime(System.currentTimeMillis());
        record.setHasDelete(false);
        record.setActivityId(activityId);
        record.setActivityContentId(activityContentId);
        create(record);
    }

    /**
     * 获得个人浏览记录
     *
     * @param userId 用户id
     * @param activityId 活动id
     * @param page 页面信息
     * @return PageData<com.maixy.studyonlineapi.view.ActivityContentBrowseRecordView>
     */
    public PageData<ActivityContentBrowseRecordView> getBrowseRecord(Long userId, Long activityId, Page page) {
        page.setHasMore(false);
        Integer beginIndex = (page.getPage() - 1) * page.getSize();
        PageData<ActivityContentBrowseRecordView> pageData = new PageData<>();
        pageData.setPage(page);
        pageData.setData(Collections.emptyList());

        List<ActivityContentBrowseRecord> activityContentBrowseRecordList = activityContentBrowseRecordMapper.getMyBrowseRecord(activityId, userId, beginIndex, page.getSize() + 1);
        if (CollectionUtils.isEmpty(activityContentBrowseRecordList)) {
            return pageData;
        }
        if (activityContentBrowseRecordList.size() > page.getSize()) {
            page.setHasMore(true);
            activityContentBrowseRecordList.remove(activityContentBrowseRecordList.size() - 1);
        }
        List<ActivityContentBrowseRecordView> activityContentBrowseRecordViewList = Lists.newArrayList();
        for (ActivityContentBrowseRecord record : activityContentBrowseRecordList) {
            ActivityContentBrowseRecordView view = new ActivityContentBrowseRecordView();
            ActivityContent activityContent = activityContentService.findById(record.getActivityContentId());
            if (null == activityContent || activityContent.getHasDelete()) {
                continue;
            }
            view.setActivityContentView(activityContentService.convert(activityContent, userId));
            view.setRecordId(DataTypeUtil.getStr(record.getId()));
            view.setCreateTimeStr(LocalDateTimeUtils.format(record.getCreateTime(), LocalDateTimeUtils.NORMAL_DATETIME_FORMAT_NO_SECOND));
            activityContentBrowseRecordViewList.add(view);
        }
        pageData.setData(activityContentBrowseRecordViewList);
        return pageData;
    }

    /**
     * 根据用户和课件
     *
     * @param userId 当前用户id
     * @param activityContentId 课件id
     * @return com.maixy.studyonlineapi.entity.ActivityContentBrowseRecord
     */
    public ActivityContentBrowseRecord findBrowseRecord(Long userId, Long activityContentId) {
        return activityContentBrowseRecordMapper.selectOne(new QueryWrapper<ActivityContentBrowseRecord>()
                .lambda()
                .eq(ActivityContentBrowseRecord::getActivityContentId, activityContentId)
                .eq(ActivityContentBrowseRecord::getCreateBy, userId)
                .eq(ActivityContentBrowseRecord::getHasDelete, false)
                .orderByDesc(ActivityContentBrowseRecord::getCreateTime)
                .last("limit 1"));
    }
}
